package cn.hdu.community.service.serviceImpl;

import cn.hdu.community.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangxin
 * @Date 2022/4/2
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ValueOperations<String, Object> opsForValue;

    //设置key的值
    @Override
    public void set(String key, Object o) {
        opsForValue.set(key, o);
    }

    //设置key的值和过期时间
    @Override
    public void set(String key, Object o, String P, Integer time) {
        switch (P) {
            case "S":     //秒位单位
                opsForValue.set(key, o, time, TimeUnit.SECONDS);
                break;
            case "M":    //分钟为单位
                opsForValue.set(key, o, time, TimeUnit.MINUTES);
                break;
            case "H":    //小时为单位
                opsForValue.set(key, o, time, TimeUnit.HOURS);
                break;
        }
    }

    //获取key的值
    @Override
    public Object get(String key) {
        return opsForValue.get(key);
    }

    //在字符串 后面追加
    @Override
    public void append(String key, Object o) {
        opsForValue.append(key, o.toString());
    }

    // 减1
    @Override
    public void decr(String key) {
        opsForValue.increment(key, -1);
    }

    //减k
    @Override
    public void decrBy(String key, long k) {
        opsForValue.increment(key, -k);
    }

    @Override
    public void decrBy(String key, double k) {
        opsForValue.increment(key, k);
    }

    //递增1
    @Override
    public void incr(String key) {
        opsForValue.increment(key, 1);
    }

    //递增n
    @Override
    public void incrBy(String key, long k) {
        opsForValue.increment(key, k);
    }

    @Override
    public void incrBy(String key, double k) {
        opsForValue.increment(key, k);
    }

    // 加k
    @Override
    public void increment(String key, long k) {
        opsForValue.increment(key, -1);
    }

    //获取 位为K的值
    @Override
    public Boolean getBit(String key, long k) {
        return opsForValue.getBit(key, k);
    }

    @Override
    public String getRange(String key, long l, long r) {
        return opsForValue.get(key, l, r);
    }

    //获取和设置
    @Override
    public Object getSet(String key, Object o) {
        return opsForValue.getAndSet(key, o);
    }

    //获取多个key 的值
    @Override
    public List<Object> mGet(List<String> list) {
        return opsForValue.multiGet(list);
    }

    //设置多个key 的值
    @Override
    public void mSet(Map<String, Object> map) {
        opsForValue.multiSet(map);
    }

    //设置多个key 的值   要么都成功 要么都失败
    @Override
    public Boolean mSetNX(Map<String, Object> map) {
        return opsForValue.multiSetIfAbsent(map);
    }

    //psetex 这个命令和 SEObjectEX 命令相似，但它以毫秒为单位设置 key 的生存时间
    @Override
    public void pSetEX(String key, Object o, long time) {
        opsForValue.set(key, o, time, TimeUnit.MILLISECONDS);
    }

    //设置偏移量
    @Override
    public Boolean setBit(String key, long k, Boolean b) {
        return opsForValue.setBit(key, k, b);
    }

    //设置存活时间 秒位单位
    @Override
    public void setEX(String key, Object o, long time) {
        opsForValue.set(key, o, time, TimeUnit.SECONDS);
    }

    //当key不存在的时候操作
    @Override
    public void setNX(String key, Object o, long time) {
        if (opsForValue.get(key) == null) opsForValue.set(key, o);
    }

    //设置偏移量位k的地方开始赋值
    @Override
    public void setRange(String key, Object o, Long k) {
        opsForValue.set(key, o, k);
    }

    //计算key的值的长度
    @Override
    public long strlen(String key) {
        return opsForValue.size(key);
    }
}
